package edu.cis084.week1;

import java.util.Scanner;

public class BasketManager
{

	public static void main(String[] args)
	{
		// declare program main variables
		boolean mainLoop=true;
		int numberOfBaskets = 10;
		int maxEggsPerBasket = 6;
		int initialAmount = 1;
		int targetBasketNum = 1;
		EggBasket[] basketArray = new EggBasket[numberOfBaskets];
		Scanner userInputStream = new Scanner(System.in);
		String userSelection;

		//Pre-initialize the array so we don't try and access a null cell later.
		for(int i=0;i < basketArray.length;i++)
		{
			basketArray[i] = new EggBasket(initialAmount,maxEggsPerBasket);
			//System.out.println("Basket " + i + " intialized.");
		};

		System.out.println("There are " + numberOfBaskets + " baskets on the table. They have eggs in them.");
		//Begin main loop.
		do
		{
			// Display the menu amd wait for input.
			System.out.println("\nWhat would you like to do?");
			System.out.println();
			System.out.print("1) Count the total number of eggs.\n2) Count the number of eggs in a basket.\n3) Add eggs to a basket.\n4) Take eggs out of a basket.\n5) Calculate the average number of eggs in each basket.\n6) Quit\n\nSelection: ");
			userSelection = userInputStream.next();
			
			//Process user input.
			switch(userSelection)
			{
				// Total number of eggs.
				case "1":
					int totalEggs = 0;

					for(int i=0;i < basketArray.length;i++)
					{
						totalEggs+=basketArray[i].getEggCount();
					};
					
					System.out.println("There are a total of " + totalEggs + " in all the baskets.");
					break;
				//Count eggs in a specific basket
				case "2":
					System.out.print("Which basket do you want to check? (1-" + numberOfBaskets + "): ");
					targetBasketNum = userInputStream.nextInt();
					System.out.println("There are " + basketArray[targetBasketNum-1].getEggCount() + " eggs in Basket #" + targetBasketNum + ".");
					break;
				//Add eggs to a target basket.
				case "3":
					int numToAdd = 0;	

					System.out.print("Which basket do you want to add eggs to? (1-" + numberOfBaskets + "): ");
					targetBasketNum = userInputStream.nextInt();
					System.out.print("How many eggs do you want to add? (1-" + maxEggsPerBasket + "): ");
					numToAdd = userInputStream.nextInt();
					basketArray[targetBasketNum-1].addEggs(numToAdd);
					break;
				//Remove eggs from a specific basket.
				case "4":
					int numToRemove = 0;
				
					System.out.print("Which basket do you want to take eggs out of? (1-" + numberOfBaskets + "): ");
					targetBasketNum = userInputStream.nextInt();
					System.out.print("How many eggs do you want to remove? (1-" + maxEggsPerBasket + "): ");
					numToRemove = userInputStream.nextInt();
					basketArray[targetBasketNum-1].removeEggs(numToRemove);
					break;
				//Find the average number of eggs in each basket. *** ASSIGNMENT TASK ***
				case "5":
					System.out.println("The average number of eggs in each basket is: " + doAverage(basketArray));
					break;
				//Quit the program by setting the main loop's continue flag to FALSE.
				case "6":
					mainLoop = false;
					break;
				//The user selected something that isn't a menu option.
				default:
					System.out.println("That is not a valid selection.");
					break;
			}
		}
		while(mainLoop==true);
		userInputStream.close();
		System.out.println("Quitting.");
	}
	
	
	// Average together the contents of all the baskets. *** ASSIGNMENT TASK ***
	public static float doAverage(EggBasket[] valuesToAverage)
	{
		float totalValue = 0;
		
		for(int i=0;i < valuesToAverage.length;i++)
		{
			totalValue+=valuesToAverage[i].getEggCount();
		}

		return totalValue/valuesToAverage.length;
	}
}
